package main

import (
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"go-http/netdisk/conf"
	"go-http/netdisk/db"
	jwt "go-http/netdisk/middleware"
	"go-http/netdisk/utils"
	"os"
	"time"
)

type claim interface {
	valid()
}

type user struct {
	name string
	age  int
}

func (u user) valid() {
	fmt.Println(u)
}

type myUser struct {
	phone string
	user  // 匿名继承可继承方法，组合（非匿名继承）不继承方法
}

func main() {

	// ============= sha1 ===============
	h := sha1.New()
	h.Write([]byte("liuxd.4719"))
	fmt.Printf("%x\n", h.Sum(nil)) // 20字节

	fmt.Println(SHA1("liuxd.4719"))
	// ============ uuid ================
	tuuid, _ := utils.NewV4()
	uuids := tuuid.String()
	fmt.Printf(uuids)

	// AddUser()

	if err := os.MkdirAll(conf.STORE_PATH+"lxd"+"/root", 0777); err != nil {
		fmt.Println(err)
	}

	fmt.Println("\n================== jwt ===========================\n")
	tokenString, err := jwt.GenerateToken("liuxd", utils.NewV4String(), 1, 0)
	if err != nil {
		fmt.Println("generate failed", err)
	} else {
		fmt.Println("generate success:", tokenString)
	}

	claims, err := jwt.ParseToken(tokenString)
	if err != nil {
		fmt.Println("ParseToken failed", err)
	} else {
		fmt.Printf("ParseToken success:\n%#v\n, %#v\n", claims)
	}
	fmt.Println("\n================== jwt ===========================\n")

	u := myUser{
		phone: "1234",
		// 不管是否为匿名继承，初始化必须带属性名user
		user: user{
			name: "lxd",
			age:  10,
		},
	}

	u.user.name = "234"
	u.name = "demo"
	u.valid()
}

func SHA1(s string) string {
	h := sha1.New()
	h.Write([]byte(s))
	return hex.EncodeToString(h.Sum(nil)) // 20 Bytes
}

// 所有字段首字母必须大写
type User2 struct {
	Uid      string
	Username string `gorm:"default:;"`
	Age      uint8  `gorm:"default:10;"`
}

// 所有字段首字母必须大写
type User struct {
	Uid    int
	Name   string `gorm:"default:'lxd';"`
	Age    uint8  `gorm:"default:100;"`
	Gender *string
}

type Test struct {
	CreateTime time.Time `gorm:"default:;"`
	UpdateTime time.Time `gorm:"default:;"`
}

func AddUser() {
	db.Init()
	defer db.DB.Close()

	u := Test{}

	if db.DB.NewRecord(&u) {
		db.DB.Create(&u)
	} else {
		fmt.Println("exist!!")
	}
}
